package com.junjiao.util.java.image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;

import com.sun.image.codec.jpeg.*;

public class WaterMark {
	private static List<Image> logoImgs = new ArrayList<Image>();
	private static List<Image> bgImgs = new ArrayList<Image>();
	private static float logoSrcOver = 0.4f;
	private static float bgSrcOver = 0.2f;

	private static boolean flag;// 为true时生成背景水印，为false时生成图标水印

	public static List getBgImgs() {
		return bgImgs;
	}

	/**
	 * 设置背景水印图片的路径并把图片加载到内存
	 * 
	 * @param bgImgUrls
	 * @param path
	 */
	public static void setBgImgs(List bgImgUrls, String path) {
		for (Iterator it = bgImgUrls.iterator(); it.hasNext();) {
			String tem = (String) it.next();
			// ImageIcon waterIcon = new ImageIcon(path + tem);
			ImageIcon waterIcon = new ImageIcon(tem);
			bgImgs.add(waterIcon.getImage());
		}
	}

	public static List getLogoImgs() {
		return logoImgs;
	}

	/**
	 * 设置图标水印图片的路径并把图片加载到内存
	 * 
	 * @param logoImgUrls
	 * @param path
	 */
	public static void setLogoImgs(List logoImgUrls, String path) {
		for (Iterator it = logoImgUrls.iterator(); it.hasNext();) {
			String tem = (String) it.next();
			ImageIcon waterIcon = new ImageIcon(path + tem);
			logoImgs.add(waterIcon.getImage());
		}
	}

	public static float getBgSrcOver() {
		return bgSrcOver;
	}

	public static void setBgSrcOver(float bgSrcOver) {
		WaterMark.bgSrcOver = bgSrcOver;
	}

	public static float getLogoSrcOver() {
		return logoSrcOver;
	}

	public static void setLogoSrcOver(float logoSrcOver) {
		WaterMark.logoSrcOver = logoSrcOver;
	}

	/**
	 * 图片中添加图标水印并输出到指定流
	 * 
	 * @param data
	 * @param out
	 * @param channel
	 * @return
	 * @throws Exception
	 */
	public static boolean createLogoMark(byte[] data, FileOutputStream out,
			String channel) throws Exception {
		int i = (int) (logoImgs.size() * Math.random());
		return createMark(data, out, (Image) logoImgs.get(i), true);
	}

	/**
	 * 图片中添加背景水印并输出到指定流
	 * 
	 * @param data
	 * @param out
	 * @param channelName
	 * @return
	 * @throws Exception
	 */
	public static boolean createBgMark(byte[] data, FileOutputStream out,
			String channelName) throws Exception {
		int i = 0;// (int)(bgImgs.size() * Math.random());
		Image waterImg = (Image) bgImgs.get(i);
		// 目标文件
		ImageIcon imgIcon = new ImageIcon(data);
		Image theImg = imgIcon.getImage();
		int width = theImg.getWidth(null);
		int height = theImg.getHeight(null);
		if (width < 10 || height < 10 || waterImg == null) {// 小图片不加水印真接输入,如头像图片
			BufferedOutputStream fout = null;
			ByteArrayInputStream in = new ByteArrayInputStream(data);
			try {
				byte[] b = new byte[1024 * 10];
				fout = new BufferedOutputStream(out);
				while (in.read(b) > 0) {
					out.write(b);
				}
				out.flush();
				out.close();
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			} finally {
				if (in != null) {
					in.close();
				}
				if (fout != null) {
					fout.close();
				}
			}
			return true;
		}

		BufferedImage bimage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = bimage.createGraphics();

		g.drawImage(theImg, 0, 0, width, height, null);
		// 水印文件
		int w = waterImg.getWidth(null);
		int h = waterImg.getHeight(null);
		g.drawImage(waterImg, width - w - 10, height - h - 10, w, h, null);
		g.dispose();
		try {
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
			param.setQuality(80f, true);
			encoder.encode(bimage);
		} catch (Exception e) {
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}
		return true;
	}

	/**
	 * 生成随机水印并输出到指定流
	 * 
	 * @param data
	 * @param out
	 * @return
	 * @throws Exception
	 */
	public static boolean createRandomMark(byte[] data, FileOutputStream out)
			throws Exception {
		int i = 0;
		Image temImg = null;
		int logoSize = logoImgs.size();
		int bgSize = bgImgs.size();
		if (logoSize <= 0 && bgSize <= 0) {
			return createMark(data, out, temImg, flag);
		}
		if (logoSize <= 0) {
			flag = true;
		} else if (bgSize <= 0) {
			flag = false;
		}
		if (!flag) {
			i = (int) (logoImgs.size() * Math.random());
			temImg = (Image) logoImgs.get(i);
		} else {
			i = (int) (bgImgs.size() * Math.random());
			temImg = (Image) bgImgs.get(i);
		}
		flag = !flag;
		return createMark(data, out, temImg, flag);
	}

	/**
	 * 生成水印并输出到指定流
	 * 
	 * @param data
	 *            图片的二进制数据
	 * @param out
	 *            处理后图片的输出流
	 * @param waterImg
	 *            水印图片的类型（背景或图标），应与isLogoImg参数一致,如果为空不加水印
	 * @param isLogoImg
	 *            等于true 时生成图标水印，否则为背景水印
	 * @return
	 * @throws Exception
	 */
	private static boolean createMark(byte[] data, FileOutputStream out,
			Image waterImg, boolean isLogoImg) throws Exception {
		ImageIcon imgIcon = new ImageIcon(data);
		Image theImg = imgIcon.getImage();

		int width = theImg.getWidth(null);
		int height = theImg.getHeight(null);
		if (width < 140 || height < 40 || waterImg == null) {// 小图片不加水印真接输入,如头像图片
			BufferedOutputStream fout = null;
			ByteArrayInputStream in = new ByteArrayInputStream(data);
			try {
				byte[] b = new byte[1024 * 10];
				fout = new BufferedOutputStream(out);
				while (in.read(b) > 0) {
					out.write(b);
				}
				out.flush();
				out.close();
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			} finally {
				if (in != null) {
					in.close();
				}
				if (fout != null) {
					fout.close();
				}
			}
			return true;
		}

		BufferedImage bimage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bimage.createGraphics();
		g.setBackground(Color.white);
		g.drawImage(theImg, 0, 0, null);

		if (isLogoImg) {// 生成图标水印
			// 设置图片透明度
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
					logoSrcOver));
			g.drawImage(waterImg, width - waterImg.getWidth(null), height
					- waterImg.getHeight(null), null);
		} else {// 生成背景水印
			// '0.2f'设置字体透明度
			// g.setComposite(AlphaComposite
			// .getInstance(AlphaComposite.SRC_OVER, bgSrcOver));
			int w = waterImg.getWidth(null);
			int h = waterImg.getHeight(null);
			if (width > w && height > h) {
				/*
				 * int sw = (width % w) /(width / w + 1);//填充水印图片横向间距 int sh =
				 * (height % h) /(height / h + 1);//填充水印图片纵向间距 for(int i = sw; i
				 * + w + sw <= width; i += w + sw) { for (int j = sh; j + h + sh
				 * <= height; j += h + sh) { g.drawImage(waterImg, i, j, null);
				 * } }
				 */

				g.drawImage(waterImg, width - w - 10, height - h - 10, w, h,
						null);
			} else {// 若上传的图片尺寸小于背景水印图片的尺寸则采用图标水印
				/*
				 * if (logoImgs != null && logoImgs.size() > 0) { Image img =
				 * (Image)logoImgs.get(0); //设置图片透明度
				 * g.setComposite(AlphaComposite
				 * .getInstance(AlphaComposite.SRC_OVER, 0.4f));
				 * g.drawImage(img, width - img.getWidth(null), height -
				 * img.getHeight(null), null); }
				 */
			}
		}
		g.dispose();
		try {
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
			param.setQuality(80f, true);
			encoder.encode(bimage);
		} catch (Exception e) {
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}
		return true;
	}
}
